/**
 * 
 */
package br.com.binding.rebind;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import br.com.binding.rebind.template.ActionListenerTemplate;
import br.com.binding.rebind.template.PropertyChangeTemplate;
import br.com.binding.rebind.template.TextBindListenerTemplate;

/**
 * @author Marcelo Emanoel
 *
 */
public class BindTypeManager {
	private static BindTypeManager instance = null;

	public static final BindTypeManager getInstance(){
		if(instance == null){
			instance = new BindTypeManager();
		}
		return instance;
	}
	
	private Map<String, List<ListenerTemplate>> templates;
	
	private BindTypeManager(){
		templates = new HashMap<String, List<ListenerTemplate>>();
		putDefaultValues();
	}
	
	private void putDefaultValues() {
		associateTypeToTemplate("com.google.gwt.user.client.ui.TextBoxBase", new TextBindListenerTemplate());
		associateTypeToTemplate("br.com.binding.client.bind.BindingModel", new PropertyChangeTemplate());
		associateTypeToTemplate("com.google.gwt.user.client.ui.ButtonBase", new ActionListenerTemplate());
//		associateTypeToTemplate("com.google.gwt.user.client.ui.ListBox", new ListBoxListenerTemplate());
	}
	
	public void associateTypeToTemplate(String typeName, ListenerTemplate template){
		if(templates.containsKey(typeName)){
			List<ListenerTemplate> templateList = templates.get(typeName);
			templateList.add(template);
		}
		else{
			List<ListenerTemplate> templateList = new ArrayList<ListenerTemplate>();
			templateList.add(template);
			templates.put(typeName, templateList);
		}
	}
	
	public void associateTypeToTemplate(String typeName, List<ListenerTemplate> templateList){
		if(templates.containsKey(typeName)){
			List<ListenerTemplate> storedList = templates.get(typeName);
			storedList.addAll(templateList);
		}
		else{
			templates.put(typeName, templateList);
		}
	}
	
	public void removeAssociation(String typeName){
		templates.remove(typeName);
	}
	
	public List<ListenerTemplate> getTemplatesForType(String type){
		return templates.get(type);
	}
	
	public List<String> getAvailableTypes(){
		return new ArrayList<String>(templates.keySet());
	}
}
